package com.shujia.spark.core

import java.lang

import org.apache.spark.rdd.RDD
import org.apache.spark.util.LongAccumulator
import org.apache.spark.{SparkConf, SparkContext}

object Demo20Acc {
  def main(args: Array[String]): Unit = {


    //环境配置对象
    val conf = new SparkConf()

    //指定任务名
    conf.setAppName("pi")
    conf.setMaster("local")


    //创建spark 环境,式写spark代码的入口
    val sc = new SparkContext(conf)


    val studentRDD: RDD[String] = sc.textFile("data/students.txt")


    var i = 0
    studentRDD.foreach(line => {
      println(line)
      i += 1

      println("算子:" + i)
    })

    println("main:" + i)

    /**
      * 累加器
      *
      */

    //1、在Driver端定义累加器
    val accumulator: LongAccumulator = sc.longAccumulator


    studentRDD.foreach(line => {

      //2、在算子内进行累加
      accumulator.add(1)
    })

    //3、在Driver端获取累加器的结果
    //注意：获取累加器的结果需要在action算子后面获取
    val count: lang.Long = accumulator.value
    println(count)
  }

}
